export interface Event {
    target: any
    listener: Function
    self?: any
}
interface Target {
    [key: string]: any
}
class EventMgr {

    private static _instance: EventMgr = new EventMgr();
    static get instance(): EventMgr {
        if (!EventMgr._instance) {
            EventMgr._instance = new EventMgr();
        }
        return EventMgr._instance;
    }
    eventList: { [key: string]: any } = {}
    on(target: Target, eventType: string, listener: Function, self?: any) {
        self = self || target;
        target['_eventListener'] = target['_eventListener'] || {}
        let event: Event = { target: target, listener: listener, self: self };
        target['_eventListener'][eventType] = event;
    }
    emit(target: Target, eventType: string, ...argv: any[]) {
        if (target['_eventListener']) {
            let event = <Event>target['_eventListener'][eventType];
            target['_eventListener'][eventType].listener.apply(event.self, argv)
        } else {
            console.error('不存在事件', eventType)
        }
    }
    off(target: Target, eventType: string) {
        target['_eventListener'] = target['_eventListener'] || {}
        target['_eventListener'][eventType] = null
    }
}
let eventMgr = new EventMgr()
export default eventMgr;
window['eventMgr'] = eventMgr;